// Copyright DustedPixels.com 2008. All rights reserved.

package com.dustedpixels.dcel.parser;

import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

/**
 * @author micapolos@gmail.com (Michal Pociecha-Los)
 */
final class AndCodeGenerator {
  static void generateAndGate(MethodVisitor methodVisitor, Context context) {
    methodVisitor.visitVarInsn(Opcodes.ILOAD, context.pinVar("IN1"));
    methodVisitor.visitVarInsn(Opcodes.ILOAD, context.pinVar("IN2"));
    methodVisitor.visitInsn(Opcodes.IAND);
    methodVisitor.visitVarInsn(Opcodes.ISTORE, context.pinVar("OUT"));
  }
  
  static void generateCombRom(MethodVisitor methodVisitor, Context context) {
    methodVisitor.visitVarInsn(Opcodes.ALOAD, context.fieldVar("dataArray"));
    methodVisitor.visitVarInsn(Opcodes.ILOAD, context.pinVar("ADDR"));
    methodVisitor.visitVarInsn(Opcodes.ILOAD, context.fieldVar("addrMask"));
    methodVisitor.visitInsn(Opcodes.IAND);
    methodVisitor.visitInsn(Opcodes.IALOAD);
    methodVisitor.visitVarInsn(Opcodes.ILOAD, context.fieldVar("dataMask"));
    methodVisitor.visitInsn(Opcodes.IAND);
    methodVisitor.visitVarInsn(Opcodes.ISTORE, context.pinVar("DATA"));
  }
  
  interface Context {
    int pinVar(String pinName);
    int fieldVar(String fieldName);
  }
}
